import  * as fs from "fs"
import * as path from "path";

export class FileEx {

    static dateDir() {
        let d = new Date();
        return "/" + d.getFullYear() + '/' + (d.getMonth() + 1) + '/' + d.getDate()
    }

    /**
     * 递归创建目录
     * @param dirname
     * @param mode
     * @returns {boolean}
     */
    static mkdirsSync(dirname: string, mode?: string) {
        if (mode === void 0) {
            mode = '0777';
        }
        if (fs.existsSync(dirname)) {
            return;
        }

        FileEx.mkdirsSync(path.dirname(dirname), mode)
        fs.mkdirSync(dirname, mode);
    }


    /**
     * 递归删除目录
     * @param path
     * @param delMenu
     */
    static deleteFolderRecursive(path: string, delMenu?: boolean) {
        if (fs.existsSync(path)) {
            let files = fs.readdirSync(path);
            files.forEach(function (file, index) {
                var curPath = path + "/" + file;
                if (fs.statSync(curPath).isDirectory()) { // recurse
                    FileEx.deleteFolderRecursive(curPath, true);
                } else { // delete file
                    fs.unlinkSync(curPath);
                }
            });
            if (delMenu)
                fs.rmdirSync(path);
        }
    };

}